package com.feidian.helper.ui;

import java.util.ArrayList;
import java.util.List;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnTouchListener;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.LinearLayout.LayoutParams;

import com.feidian.helper.R;
import com.feidian.helper.adapter.PagerItemAdapter;

public class GuidePageActivity extends Activity implements
		OnPageChangeListener, OnTouchListener {
	private static final String SHAREDPREFERENCES_NAME = "first_pref";
	private ViewPager vp_guide;
	private List<View> pageList;
	private LinearLayout layout_dotView;
	private ImageView[] imgDots;
	private int dotCount;
	private int currentPage = 0;
	private int lastX = 0;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_guide_page);
		initView();
		initDots();
		setPage();
	}

	private void initView() {
		layout_dotView = (LinearLayout) findViewById(R.id.layout_dotView);
		vp_guide = (ViewPager) findViewById(R.id.vp_guide);
		vp_guide.setOnPageChangeListener(this);
		vp_guide.setOnTouchListener(this);
		vp_guide.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				goHome();
				setGuide();
			}
		});
		pageList = initGuidePageList(this);
		dotCount = pageList.size();
	}

	private static List<View> initGuidePageList(Context context) {
		List<View> pageList = new ArrayList<View>();
		pageList.add(getPageView(context, R.drawable.guide_page01));
		pageList.add(getPageView(context, R.drawable.guide_page02));
		pageList.add(getPageView(context, R.drawable.guide_page03));
		//pageList.add(getPageView(context, R.drawable.guide_page04));
		return pageList;
	}

	private static View getPageView(Context context, int imgResId) {
		LayoutInflater inflater = LayoutInflater.from(context);
		View pageView = inflater.inflate(R.layout.page_item, null);
		ImageView imgPage = (ImageView) pageView.findViewById(R.id.imgPage);
		imgPage.setBackgroundResource(imgResId);
		return pageView;
	}

	private void initDots() {
		imgDots = new ImageView[dotCount];
		for (int i = 0; i < dotCount; i++) {
			ImageView dotView = new ImageView(this);
			if (i == 0) {
				dotView.setBackgroundResource(R.drawable.dot_white);
			} else {
				dotView.setBackgroundResource(R.drawable.dot_gray);
			}
			imgDots[i] = dotView;
			LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
					LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
			params.setMargins(7, 0, 7, 0);
			dotView.setLayoutParams(params);
			layout_dotView.addView(dotView);
		}
	}

	private void setPage() {
		vp_guide.setAdapter(new PagerItemAdapter(pageList));
	}

	@Override
	public void onPageSelected(int arg0) {
		currentPage = arg0;
		for (int i = 0; i < dotCount; i++) {
			if (i == arg0) {
				imgDots[i].setBackgroundResource(R.drawable.dot_white);
			} else {
				imgDots[i].setBackgroundResource(R.drawable.dot_gray);
			}
		}
	}

	@Override
	public void onPageScrollStateChanged(int arg0) {

	}

	@Override
	public void onPageScrolled(int arg0, float arg1, int arg2) {

	}

	@Override
	public boolean onTouch(View v, MotionEvent event) {
		switch (event.getAction()) {
		case MotionEvent.ACTION_DOWN:
			lastX = (int) event.getX();
			if ((currentPage == pageList.size() - 1)) {
				goHome();
				setGuide();
			}
			break;
		case MotionEvent.ACTION_MOVE:
			if ((lastX - event.getX()) > 100
					&& (currentPage == pageList.size() - 1)) {
				goHome();
				setGuide();
			}
			break;
		default:
			break;
		}
		return false;
	}

	public void goHome() {
		startActivity(new Intent(this,MainFragmentActivity.class));
		this.finish();
	}

	public void setGuide() {
		SharedPreferences preferences = this.getSharedPreferences(
				SHAREDPREFERENCES_NAME, Context.MODE_PRIVATE);
		preferences.edit().putBoolean("isFirstIn", false).commit();
	}

}
